//
//  SortDetailedViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SortDetailedViewController.h"
#import "SortDetailed.h"
#import "SortDetailedCollectionViewCell.h"
#import "SortOtherCollectionViewCell.h"
#import "SortTopViewController.h"
#import "SortMoreViewController.h"
#import "FindViewController.h"
#import "MusicListViewController.h"
#import "SortLaoShuShiViewController.h"
#import "SortGDCollectionViewCell.h"
#import "BestMusicListViewController.h"
@interface SortDetailedViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic ,strong)NSMutableDictionary *dic;
@property (nonatomic ,strong)UIScrollView *scrollView;
@property (nonatomic ,strong)NSMutableArray *CaiDanArr;
@property (nonatomic ,strong)UIButton *button;
@property (nonatomic ,strong)UIButton *CaiDanButton;
@property (nonatomic ,strong)UICollectionView *collectionView;
@property (nonatomic ,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic ,assign)NSInteger integer;
@property (nonatomic ,strong)NSMutableArray *buttonArr;
@property (nonatomic ,strong)UIView *viewOwn;
@property (nonatomic ,strong)UICollectionView *collectionGD;
@property (nonatomic ,strong)UICollectionViewFlowLayout *flowLayoutGD;
@end

@implementation SortDetailedViewController
-(void)netWorking
{
    [NetWorkManager GET:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%@&contentType=album&device=iPhone&scale=2&version=4.3.20",self.num] isCaChe:NO resultData:^(id responseObject) {
        self.dic=responseObject;
        NSMutableArray *arrr=[[self.dic objectForKey:@"tags"] objectForKey:@"list"];
        self.CaiDanArr=[NSMutableArray array];
        for (NSMutableDictionary *dic in arrr) {
            SortDetailed *detailed=[[SortDetailed alloc]init];
            [detailed setValuesForKeysWithDictionary:dic];
            [self.CaiDanArr addObject:detailed];
        }

        SortDetailed *sort = [[SortDetailed alloc] init];
        sort.tname = @"推荐";
        sort.category_id = @3;
        [self.CaiDanArr insertObject:sort atIndex:0];
        
        
        [self arr];
        [self.collectionGD reloadData];
        [self.collectionView reloadData];
    } resultRrror:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}
-(void)arr
{

    self.title=self.str;
    self.view.backgroundColor=[UIColor whiteColor];
    self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, FM_Screen_Width/5*4, FM_Screen_Pts_Height*35)];

    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(self.CaiDanArr.count*self.scrollView.bounds.size.width/3, 0);
    self.buttonArr=[NSMutableArray array];
    for (NSInteger i=0; i<self.CaiDanArr.count; i++) {
       UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
       button.frame=CGRectMake(i*self.scrollView.bounds.size.width/3, 0, self.scrollView.bounds.size.width/3, self.scrollView.bounds.size.height);
        NSString *str= [[self.CaiDanArr objectAtIndex:i] tname];

     button.tag=20000+i;
        
         [button addTarget:self action:@selector(buttonScroll:) forControlEvents:UIControlEventTouchUpInside];
        /////
        [button setTintColor:[UIColor blackColor]];
        [button setTitle:str forState:UIControlStateNormal];
        
        if (i==0) {
            [button setTintColor:[UIColor orangeColor]];
            self.integer=20000;
        }
        [self.scrollView addSubview: button];
        [self.buttonArr addObject:button];
    }
        ///
        self.CaiDanButton=[UIButton buttonWithType:UIButtonTypeSystem];
        self.CaiDanButton.frame=CGRectMake(FM_Screen_Width/5*4, 64, FM_Screen_Width/5, FM_Screen_Pts_Height*35);
        [self.CaiDanButton setTitle:@"更多" forState:UIControlStateNormal];
    [self.CaiDanButton addTarget:self action:@selector(buttonMore:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.CaiDanButton];
        
        
        self.flowLayout=[[UICollectionViewFlowLayout alloc]init];
        self.collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 64+FM_Screen_Pts_Height*35, FM_Screen_Width, FM_Screen_Height-64-FM_Screen_Pts_Height*35) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor=[UIColor lightGrayColor];
        self.collectionView.delegate=self;
        self.collectionView.dataSource=self;
    self.collectionView.tag=20801;
    
       self.flowLayout.itemSize=CGSizeMake
        (FM_Screen_Width,FM_Screen_Height-64-FM_Screen_Pts_Height*35);
    
       self.flowLayout.minimumLineSpacing=0;
        self.collectionView.pagingEnabled = YES;
    
   
       self.flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
        [self.view addSubview:self.collectionView];
        
        [self.collectionView registerClass:[SortDetailedCollectionViewCell class] forCellWithReuseIdentifier:@"sortDetailde"];
    
        [self.collectionView registerClass:[SortOtherCollectionViewCell class] forCellWithReuseIdentifier:@"other"];

}
-(void)buttonScroll:(UIButton *)bu
{
    
   NSInteger i=bu.tag-20000;
    self.collectionView.contentOffset=CGPointMake(i*FM_Screen_Width, 0);
 
    UIButton *old=[self.buttonArr objectAtIndex:(self.integer-20000)];
    [old setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    UIButton *new=[self.buttonArr objectAtIndex:i];
    [new setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.integer=bu.tag;

}
// UIScrollViewDelegate代理方法,根据偏移量设置menu选中状态
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / FM_Screen_Width;
    if (index != self.integer) {
        // 还原menu颜色
        UIButton *oldMenuBtn = [self.buttonArr objectAtIndex:(self.integer-20000)];
        [oldMenuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        // 设置menu颜色
        UIButton *menuBtn = [self.buttonArr objectAtIndex:index];
        [menuBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.integer = 20000 + index;
        
        // 更新位置提示
        if (index-1>=0) {
            self.scrollView.contentOffset=CGPointMake(self.scrollView.bounds.size.width/3*(index-1), 0);

        }else{
           self.scrollView.contentOffset=CGPointMake(self.scrollView.bounds.size.width/3*index, 0);
        }
        
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor lightGrayColor];

     [self netWorking];
    
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor=[UIColor whiteColor];
    self.viewOwn=[[UIView alloc]init];
    self.viewOwn.backgroundColor=[UIColor lightGrayColor];
    self.viewOwn.frame=CGRectMake(0, 0, FM_Screen_Width, 0);

    [self.view addSubview:self.viewOwn];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView ==self.collectionGD) {
        return self.CaiDanArr.count;
    }else if (collectionView ==self.collectionView){
    return self.CaiDanArr.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView ==self.collectionGD) {
    
        SortGDCollectionViewCell *cell1=[collectionView dequeueReusableCellWithReuseIdentifier:@"gd" forIndexPath:indexPath];

        SortDetailed *sort=[self.CaiDanArr objectAtIndex:indexPath.item];
        
        cell1.lableGD.text=sort.tname;
        cell1.lableGD.textAlignment=YES;
        
        return cell1;
    }else if(collectionView==self.collectionView){
    if (indexPath.item==0) {
        SortDetailedCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"sortDetailde" forIndexPath:indexPath];
        
        cell.arr=[[self.dic objectForKey:@"categoryContents"] objectForKey:@"list"];
        
        
        void(^block1)(void)=^(void){
            SortTopViewController *top=[[SortTopViewController alloc]init];
           top.str=[[[[[self.dic objectForKey:@"tags"] objectForKey:@"list"]
                      objectAtIndex:0] objectForKey:@"category_id"] stringValue];
            
            [self.navigationController pushViewController:top animated:YES];
        };
        
        
        void(^blocklao)(NSString *str)=^(NSString *str){
            SortLaoShuShiViewController *lao=[[SortLaoShuShiViewController alloc]init];
            lao.str=str;
            lao.strId=[[[[self.dic objectForKey:@"tags"] objectForKey:@"list"] objectAtIndex:0] objectForKey:@"category_id"];
            [self.navigationController pushViewController:lao animated:YES];
        };
        
        cell.blocklao=blocklao;

        void(^BlockLaoShU)(NSMutableDictionary *dic)=^(NSMutableDictionary *dic){
            BestMusicListViewController *best=[[BestMusicListViewController alloc]init];
            best.bestMusicDictionary=dic;
            [self.navigationController pushViewController:best animated:YES];
            
        };
        cell.blockLaoShuShi=BlockLaoShU;
        
        cell.block=block1;
        void(^block2)(NSString *str,NSString *strstr)=^(NSString *str,NSString *strstr){
            SortMoreViewController *more=[[SortMoreViewController alloc]init];

            
            if ( ![[[[[self.dic objectForKey:@"categoryContents"] objectForKey:@"list"] objectAtIndex:indexPath.section] objectForKey:@"title"] isEqualToString:@"排行榜"]) {
               more.str1=strstr;
               more.str=str;
               more.strId=[[[[[self.dic objectForKey:@"tags"] objectForKey:@"list"] objectAtIndex:0] objectForKey:@"category_id"] stringValue];
               
            }else{
          
            more.str= str;
            more.str1=strstr;
            more.strId=[[[[[self.dic objectForKey:@"tags"] objectForKey:@"list"] objectAtIndex:0] objectForKey:@"category_id"] stringValue];
            }
            [self.navigationController pushViewController:more animated:YES];
        };
        cell.block1=block2;
        
        void(^block3)(NSMutableDictionary *dic)=^(NSMutableDictionary *dic){
           
            MusicListViewController *find=[[MusicListViewController alloc]init];

            find.musicListDic=dic;
            [self.navigationController pushViewController:find animated:YES];
            
            
        };
        cell.blockzheng=block3;
        
         return cell;
    }else{
        
      SortOtherCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        cell.num=[[[[self.dic objectForKey:@"tags"] objectForKey:@"list"] objectAtIndex:indexPath.item-1] objectForKey:@"category_id"];
      cell.str =[[[[self.dic objectForKey:@"tags"] objectForKey:@"list"] objectAtIndex:indexPath.item-1] objectForKey:@"tname"];

        void(^block)(NSMutableDictionary *dic)=^(NSMutableDictionary *dic){
           
            MusicListViewController *find=[[MusicListViewController alloc]init];
            
            find.musicListDic=dic;
            [self.navigationController pushViewController:find animated:YES];

        };
        cell.block=block;
        return cell;
      }
    }
    return NULL;
}
-(void)buttonMore:(UIButton *)bu
{

    if ([bu.titleLabel.text isEqualToString:@"更多"]) {
        [UIView animateWithDuration:0.2 animations:^{
            [self gengDuo];
            self.flowLayoutGD=[[UICollectionViewFlowLayout alloc]init];
            
            self.collectionGD=[[UICollectionView alloc]initWithFrame:CGRectMake(0, FM_Screen_Pts_Width*50, FM_Screen_Width, self.viewOwn.bounds.size.height-FM_Screen_Pts_Height*50) collectionViewLayout:self.flowLayoutGD];
            self.collectionGD.delegate=self;
            self.collectionGD.dataSource=self;
            self.collectionGD.backgroundColor=[UIColor lightGrayColor];
            self.collectionGD.tag=20800;
            
            self.flowLayoutGD.itemSize=CGSizeMake(FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*40);
           self.flowLayoutGD.minimumLineSpacing=20;
            self.flowLayoutGD.minimumInteritemSpacing=20;
            self.flowLayoutGD.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);

            self.flowLayoutGD.scrollDirection=UICollectionViewScrollDirectionVertical;
            
            [self.viewOwn addSubview:self.collectionGD];
            
            [self.collectionGD registerClass:[SortGDCollectionViewCell class] forCellWithReuseIdentifier:@"gd"];

            
         [bu setTitle:@"收回" forState:UIControlStateNormal];
        }];

    }else{
        [UIView animateWithDuration:0.2 animations:^{
            
            [self shouHui];
        [bu setTitle:@"更多" forState:UIControlStateNormal];
        }];

        
        
    }
    
    
    
}
-(void)gengDuo
{
    self.viewOwn.alpha=1;

    self.viewOwn.frame=CGRectMake(0, 64, FM_Screen_Width, FM_Screen_Height-64);
    self.collectionGD.alpha=1;
    self.collectionView.alpha=0;
    self.scrollView.alpha=0;
 
}
-(void)shouHui
{
    self.viewOwn.alpha=0;

    self.viewOwn.frame=CGRectMake(0, 0, FM_Screen_Width, 0);
    self.collectionGD.alpha=0;
    self.collectionView.alpha=1;
    self.scrollView.alpha=1;

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"11111111%ld",indexPath.row);
    
    
    self.collectionView.contentOffset=CGPointMake(FM_Screen_Width*indexPath.row, 0);
    [self shouHui];
    [self.CaiDanButton setTitle:@"更多" forState:UIControlStateNormal];
    
    
    if (indexPath.row-1>=0) {
        self.scrollView.contentOffset=CGPointMake(self.scrollView.bounds.size.width/3*(indexPath.row-1), 0);
        
    }else{
        self.scrollView.contentOffset=CGPointMake(self.scrollView.bounds.size.width/3*indexPath.row, 0);
    }
    
    NSInteger index = self.scrollView.contentOffset.x / FM_Screen_Width;
    
    
    if (index != self.integer) {
        // 还原menu颜色
        UIButton *oldMenuBtn = [self.buttonArr objectAtIndex:(self.integer-20000)];
        [oldMenuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        // 设置menu颜色
        UIButton *menuBtn = [self.buttonArr objectAtIndex:index];
        [menuBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.integer = 20000 + index;
        
    }

    
    
    
    NSInteger i=indexPath.row;
    self.collectionView.contentOffset=CGPointMake(i*FM_Screen_Width, 0);
    
    UIButton *old=[self.buttonArr objectAtIndex:(self.integer-20000)];
    [old setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIButton *new=[self.buttonArr objectAtIndex:i];
    [new setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.integer=indexPath.row+20000;


    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
